home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / objarray.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  2KB  |  84 lines

  1.                                  // Chapter 6 - Program 1
  2. #include <iostream.h>
  3.  
  4. class box {
  5.    int length;
  6.    int width;
  7.    static int extra_data;   // Declaration of extra_data
  8. public:
  9.    box(void);             //Constructor
  10.    void set(int new_length, int new_width);
  11.    int get_area(void);
  12.    int get_extra(void) {return extra_data++;}
  13. };
  14.  
  15.  
  16. int box::extra_data;        // Definition of extra_data
  17.  
  18.  
  19. box::box(void)        //Constructor implementation
  20. {
  21.    length = 8;
  22.    width = 8;
  23.    extra_data = 1;
  24. }
  25.  
  26.  
  27. // This method will set a box size to the two input parameters
  28. void box::set(int new_length, int new_width)
  29. {
  30.    length = new_length;
  31.    width = new_width;
  32. }
  33.  
  34.  
  35. // This method will calculate and return the area of a box instance
  36. int box::get_area(void)
  37. {
  38.    return (length * width);
  39. }
  40.  
  41.  
  42. main()
  43. {
  44. box small, medium, large, group[4];        //Seven boxes to work with
  45.  
  46.    small.set(5, 7);
  47.    large.set(15, 20);
  48.    
  49.    for (int index = 1 ; index < 4 ; index++)  //group[0] uses default
  50.       group[index].set(index + 10, 10);
  51.  
  52.    cout << "The small box area is " << small.get_area() << "\n";
  53.    cout << "The medium box area is " << medium.get_area() << "\n";
  54.    cout << "The large box area is " << large.get_area() << "\n";
  55.  
  56.    for (index = 0 ; index < 4 ; index++)
  57.       cout << "The array box area is " << 
  58.                                      group[index].get_area() << "\n";
  59.  
  60.    cout << "The extra data value is " << small.get_extra() << "\n";
  61.    cout << "The extra data value is " << medium.get_extra() << "\n";
  62.    cout << "The extra data value is " << large.get_extra() << "\n";
  63.    cout << "The extra data value is " << group[0].get_extra() << "\n";
  64.    cout << "The extra data value is " << group[3].get_extra() << "\n";
  65. }
  66.  
  67.  
  68.  
  69.  
  70. // Result of execution
  71. //
  72. // The small box area is 35
  73. // The medium box area is 64
  74. // The large box area is 300
  75. // The array box area is 64
  76. // The array box area is 110
  77. // The array box area is 120
  78. // The array box area is 130
  79. // The extra data value is 1
  80. // The extra data value is 2
  81. // The extra data value is 3
  82. // The extra data value is 4
  83. // The extra data value is 5
  84.